{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Matrix Diagonal Sum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: diagonalSum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #矩阵对角线元素的和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个正方形矩阵 <code>mat</code>，请你返回矩阵对角线元素的和。</p>\n",
    "\n",
    "<p>请你返回在矩阵主对角线上的元素和副对角线上且不在主对角线上元素的和。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例&nbsp; 1：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/08/14/sample_1911.png\" style=\"height:174px; width:336px\" /></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>mat = [[<strong>1</strong>,2,<strong>3</strong>],\n",
    "&nbsp;           [4,<strong>5</strong>,6],\n",
    "&nbsp;           [<strong>7</strong>,8,<strong>9</strong>]]\n",
    "<strong>输出：</strong>25\n",
    "<strong>解释：</strong>对角线的和为：1 + 5 + 9 + 3 + 7 = 25\n",
    "请注意，元素 mat[1][1] = 5 只会被计算一次。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例&nbsp; 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>mat = [[<strong>1</strong>,1,1,<strong>1</strong>],\n",
    "&nbsp;           [1,<strong>1</strong>,<strong>1</strong>,1],\n",
    "&nbsp;           [1,<strong>1</strong>,<strong>1</strong>,1],\n",
    "&nbsp;           [<strong>1</strong>,1,1,<strong>1</strong>]]\n",
    "<strong>输出：</strong>8\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>mat = [[<strong>5</strong>]]\n",
    "<strong>输出：</strong>5\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>n == mat.length == mat[i].length</code></li>\n",
    "\t<li><code>1 &lt;= n &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= mat[i][j] &lt;= 100</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [matrix-diagonal-sum](https://leetcode.cn/problems/matrix-diagonal-sum/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [matrix-diagonal-sum](https://leetcode.cn/problems/matrix-diagonal-sum/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[1,2,3],[4,5,6],[7,8,9]]', '[[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1]]', '[[5]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n=len(mat)\n",
    "        ans=0\n",
    "\n",
    "        for i,row in enumerate(mat):\n",
    "            j=n-i-1\n",
    "            ans+=row[i]+(0 if j==i else row[j])\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        total = 0\n",
    "        mid = n // 2\n",
    "        for i in range(n):\n",
    "            total += mat[i][i] + mat[i][n - 1 - i]\n",
    "        \n",
    "        return total - mat[mid][mid] * (n & 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            res += mat[i][i]\n",
    "            if n - 1 - i != i:\n",
    "                res += mat[i][n - 1 - i]\n",
    "        # if n % 2 == 1:\n",
    "        #     res -= mat[n//2][n//2]\n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "\n",
    "        ln = len(mat)\n",
    "        s1 = 0\n",
    "        s2 = 0\n",
    "        m = 0\n",
    "        for i,j in zip(range(ln),range(ln-1,-1,-1)):\n",
    "            if  i == j:\n",
    "                m = mat[i][j]\n",
    "            s1 += mat[i][i]\n",
    "            s2 += mat[i][j]\n",
    "        \n",
    "        return s1+s2-m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        sum = 0\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if i == j or i + j == n-1:\n",
    "                    sum += mat[i][j]\n",
    "        \n",
    "        # if n % 2 == 1:\n",
    "        #     sum -= mat[n//2][n//2]\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        width=len(mat)-1\n",
    "        ans=0\n",
    "        for i in range(0,width+1):\n",
    "            ans+=mat[0+i][0+i]+mat[i][width-i]\n",
    "        if (width+2)%2==0:\n",
    "            ans-=mat[width>>1][width>>1]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        ans = 0\n",
    "        n = len(mat)\n",
    "        i,j = 0,n-1\n",
    "        while i<n and j>=0:\n",
    "            ans += mat[i][i]\n",
    "            if j!=i:\n",
    "                ans += mat[i][j]\n",
    "            j -= 1\n",
    "            i += 1\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        l = len(mat)\n",
    "        res = 0\n",
    "        for i in range(l):\n",
    "            res += mat[i][i]\n",
    "        for i in range(l):\n",
    "            res += mat[i][l-1-i]\n",
    "        \n",
    "        if (l%2==0):\n",
    "            return res\n",
    "        else:\n",
    "            return res-mat[l//2][l//2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        result = 0\n",
    "        for i in range(0, n):\n",
    "            result += mat[i][i]\n",
    "            result += mat[i][n - i - 1]\n",
    "        if n % 2 == 1:\n",
    "            result -= mat[n // 2][n // 2]\n",
    "        return result\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "\n",
    "        s = 0\n",
    "\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if i == j or i + j == n - 1:\n",
    "                    s += mat[i][j]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        i = j = 0\n",
    "        length = len(mat)\n",
    "        sum = 0\n",
    "        while i < length:\n",
    "            sum+=mat[i][j]\n",
    "            sum+=mat[length-i-1][j]\n",
    "            i+=1\n",
    "            j+=1\n",
    "        if length%2:\n",
    "            sum -= mat[length//2][length//2]\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        a = len(mat)\n",
    "        i, j, l, r  = 0, 0, 0, a-1\n",
    "        res = 0\n",
    "        for _ in range(a):\n",
    "            res += mat[i][j]\n",
    "            res += mat[l][r]\n",
    "            i += 1\n",
    "            j += 1 \n",
    "            l += 1\n",
    "            r -= 1\n",
    "            \n",
    "        if a % 2 == 1:\n",
    "            res -= mat[a//2][a//2]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        length = len(mat)\n",
    "        ans = 0\n",
    "        for i in range(length):\n",
    "            ans += mat[i][i]\n",
    "            ans += mat[i][length - i - 1]\n",
    "        if length % 2 != 0:\n",
    "            ans -= mat[length // 2][length // 2]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        ans = 0\n",
    "        n = len(mat)\n",
    "        for i, row in enumerate(mat):\n",
    "            j = n - i - 1\n",
    "            ans += row[i] + (0 if j == i else row[j])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        diag_sum = 0\n",
    "        for i in range(0,n):\n",
    "            diag_sum += mat[i][i]\n",
    "            diag_sum += mat[i][n-1-i]\n",
    "        if n % 2 != 0:\n",
    "            diag_sum -= mat[(n-1)//2][(n-1)//2]\n",
    "        return diag_sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n=len(mat)\n",
    "        ans=0\n",
    "        for i in range(n):\n",
    "            ans+=(mat[i][i]+mat[i][n-1-i])\n",
    "        if n%2==0:\n",
    "            return ans\n",
    "        else:\n",
    "            return ans-mat[i//2][i//2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        l = len(mat)\n",
    "        s = 0\n",
    "        dic = {}\n",
    "        for i in range(l):\n",
    "            # s = s + mat[i][i] + mat[i][abs(i-(l-1))]\n",
    "            dic[(i,i)] = mat[i][i]\n",
    "            dic[(i,abs(i-(l-1)))] = mat[i][abs(i-(l-1))]\n",
    "        # for i,j in dic.keys():\n",
    "        #     s += mat[i][j]\n",
    "        # return s\n",
    "        return sum([x for x in dic.values()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        sum = 0\n",
    "        mid = n // 2\n",
    "        for i in range(n):\n",
    "            sum += mat[i][i] + mat[i][n-1-i]\n",
    "        return sum - mat[mid][mid] * (n & 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        a = [mat[i][i] for i in range(len(mat))]\n",
    "        b = [mat[i][-(i + 1)] for i in range(len(mat))]\n",
    "        if len(mat) % 2:\n",
    "            return sum(a) + sum(b) - a[len(mat)//2]\n",
    "        else:\n",
    "            return sum(a) + sum(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        res = 0\n",
    "        l = len(mat)\n",
    "        \n",
    "        for i in range(l):\n",
    "            j1 = l - i - 1\n",
    "            j2 = i\n",
    "            res += mat[i][j1] + mat[i][j2]\n",
    "        center = mat[l//2][l//2]\n",
    "        return res - center if l%2 != 0 else res\n",
    "        \n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        y1 = 0\n",
    "        y2 = len(mat) - 1\n",
    "        x = 0\n",
    "        result = 0\n",
    "        while x != len(mat):\n",
    "            if y1 == y2:\n",
    "                result += mat[y1][x]\n",
    "            else:\n",
    "                result = result + mat[y1][x] + mat[y2][x]\n",
    "            y1 += 1\n",
    "            y2 -= 1\n",
    "            x += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n=len(mat)\n",
    "        sum=0\n",
    "        for i in range(n):\n",
    "            if i!=n-1-i:\n",
    "                sum+=mat[i][i]+mat[i][n-1-i]\n",
    "            else:\n",
    "                sum+=mat[i][i]\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        i = j = 0\n",
    "        length = len(mat)\n",
    "        sum = 0\n",
    "        while i < length:\n",
    "            sum+=mat[i][j]\n",
    "            sum+=mat[length-i-1][j]\n",
    "            i+=1\n",
    "            j+=1\n",
    "        if length%2:\n",
    "            sum -= mat[length//2][length//2]\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            res += mat[i][i]\n",
    "            res += mat[-(i+1)][i]\n",
    "        if n%2==1:\n",
    "            res -= mat[(n-1)//2][(n-1)//2]\n",
    "        return res\n",
    "        \n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            res += mat[i][i] + mat[i][n - 1 - i]\n",
    "        \n",
    "        if n % 2 != 0:\n",
    "            res -= mat[n // 2][n // 2]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        count = 0\n",
    "        for i in range(len(mat)):\n",
    "            count += mat[i][i]\n",
    "            if i != len(mat)-1-i:\n",
    "                count += mat[i][len(mat)-1-i]\n",
    "\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        s = 0\n",
    "        for i in range(len(mat)):\n",
    "            s += mat[i][i]\n",
    "            if i != len(mat)-i-1:\n",
    "                s += mat[i][len(mat)-i-1]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        res = 0\n",
    "        x, y = 0, 0\n",
    "        while x < len(mat):\n",
    "            res += mat[y][x]\n",
    "            x += 1\n",
    "            y += 1\n",
    "        x, y = len(mat) - 1, 0\n",
    "        while x >= 0:\n",
    "            res += mat[y][x]\n",
    "            y += 1\n",
    "            x -= 1\n",
    "        if len(mat) % 2 == 0:\n",
    "            return res\n",
    "        else:\n",
    "            return res - mat[len(mat) // 2][len(mat) // 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        sum = 0\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if i == j or i + j == n - 1:\n",
    "                    sum += mat[i][j]\n",
    "        return sum\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        i = j = 0\n",
    "        length = len(mat)\n",
    "        sum = 0\n",
    "        while i < length:\n",
    "            sum+=mat[i][j]\n",
    "            sum+=mat[length-i-1][j]\n",
    "            i+=1\n",
    "            j+=1\n",
    "        if length%2:\n",
    "            sum -= mat[length//2][length//2]\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        diagonal_sum = 0\n",
    "\n",
    "        for i in range(n):\n",
    "            diagonal_sum += mat[i][i]  # 累加主对角线上的元素\n",
    "            diagonal_sum += mat[i][n - i - 1]  # 累加副对角线上的元素\n",
    "\n",
    "    # 若矩阵维度为奇数，则副对角线上的中间元素会被重复累加，需要减去重复部分\n",
    "        if n % 2 == 1:\n",
    "            diagonal_sum -= mat[n // 2][n // 2]\n",
    "        return diagonal_sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        mats=mat[::-1]\n",
    "        a=[]\n",
    "        b=[]\n",
    "        for i in range(len(mat)):\n",
    "            a.append(mat[i][i])\n",
    "            a.append(mats[i][i])\n",
    "            if len(mat)%2==1:\n",
    "                # if mat[i][i]==mats[i][i]:\n",
    "                    b.append(mat[(len(mat)-1)//2][(len(mat)-1)//2])\n",
    "        if len(b)==0:\n",
    "            return sum(a)\n",
    "        else:\n",
    "            return (sum(a)-b[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        res=[[0 for _ in range(len(mat[0]))] for _ in range(len(mat))]\n",
    "        for i in range(len(mat)):\n",
    "            for j in range(len(mat[0])):\n",
    "                if i==j or i+j==len(mat)-1:\n",
    "                    res[i][j]=mat[i][j]        \n",
    "        msum=sum([sum(x) for x in res])\n",
    "        return msum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        l = len(mat[0])\n",
    "        res = 0\n",
    "        for i in range(l):\n",
    "            for j in range(l):\n",
    "                if i == j:\n",
    "                    res += mat[i][j]\n",
    "                if i + j == l - 1:\n",
    "                    res += mat[i][j]\n",
    "        # 若矩阵的阶数是奇数阶，则中心的对角线元素重复计数\n",
    "        if l % 2 == 1:\n",
    "            res -= mat[int((l-1)/2)][int((l-1)/2)]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        if len(mat) == 1:\n",
    "            return mat[0][0]\n",
    "        res = 0\n",
    "        for i in range(len(mat)):\n",
    "            res += mat[i][i]\n",
    "            res += mat[len(mat)-i-1][i]\n",
    "        if len(mat) % 2 != 0:\n",
    "            res -= mat[len(mat)//2][len(mat)//2]\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        ans = 0\n",
    "        n = len(mat)\n",
    "        for i, row in enumerate(mat):\n",
    "            j = n - i - 1\n",
    "            ans += row[i] + (0 if i == j else row[j])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        sum1 = 0\n",
    "        sum2 = 0\n",
    "        for i in range(n):\n",
    "            sum1+=mat[i][i]\n",
    "            sum2+=mat[i][n-1-i]\n",
    "        sum = sum1+sum2\n",
    "        if n%2==1:\n",
    "            sum-=mat[n//2][n//2]\n",
    "\n",
    "        return sum\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        q=len(mat)\n",
    "        n=q//2+(q&1)\n",
    "        s=0\n",
    "        for i in range(n):\n",
    "            if i+1!=q-i:\n",
    "                s+=mat[i][i]\n",
    "                s+=mat[i][-1-i]\n",
    "                s+=mat[-1-i][i]\n",
    "                s+=mat[-1-i][-1-i]\n",
    "            else:\n",
    "                s+=mat[i][i]\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n,res = len(mat),0\n",
    "        for i in range(n):\n",
    "            res += mat[i][i]\n",
    "            mat[i][i] = 0\n",
    "            res += mat[i][n-i-1]\n",
    "        \n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        mat_len = len(mat)\n",
    "        result = 0\n",
    "        for i in range(mat_len):\n",
    "            result += mat[i][i]\n",
    "            result += mat[mat_len - 1 - i][i]\n",
    "        if mat_len % 2 != 0:\n",
    "            index = int(mat_len / 2)\n",
    "            result -= mat[index][index]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        res = 0\n",
    "        for i in range(n):\n",
    "            res += mat[i][i]\n",
    "            res += mat[i][n - 1 - i]\n",
    "        return res - mat[n//2][n//2] if n & 1 else res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        total = 0\n",
    "        mid = n // 2\n",
    "        for i in range(n):\n",
    "            total += mat[i][i] + mat[i][n - 1 - i]\n",
    "        return total - mat[mid][mid] * (n & 1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        path = []\n",
    "        place = 0\n",
    "        add = 0\n",
    "        for i in range(len(mat)):\n",
    "            a = mat[i][place]\n",
    "            add += a\n",
    "            path.append([i, place])\n",
    "            place += 1\n",
    "        place -= 1\n",
    "        for j in range(len(mat)):\n",
    "            b = mat[j][place]\n",
    "            if not ([j, place]) in path:\n",
    "                add += b\n",
    "            place -= 1\n",
    "            path.append([j, place])\n",
    "        return add\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        ans = 0\n",
    "        n = len(mat)\n",
    "        for i, row in enumerate(mat):\n",
    "            j = n - i - 1\n",
    "            ans += row[i] + (0 if j == i else row[j])\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        Sum=0\n",
    "        for i in range(len(mat)):\n",
    "            for j in range(len(mat)):\n",
    "                if i==j:\n",
    "                    Sum+=mat[i][j]\n",
    "                elif i+j==len(mat)-1:\n",
    "                    Sum+=mat[i][j]\n",
    "        return Sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        total = 0\n",
    "        for i in range(n):\n",
    "            total += (mat[i][i])\n",
    "            total += (mat[i][n-i-1])\n",
    "        if n%2==1:\n",
    "            total -= mat[n//2][n//2]\n",
    "        return total"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        l = len(mat)\n",
    "        dic = {}\n",
    "        for i in range(l):\n",
    "            # s = s + mat[i][i] + mat[i][abs(i-(l-1))]\n",
    "            dic[(i,i)] = mat[i][i]\n",
    "            dic[(i,abs(i-(l-1)))] = mat[i][abs(i-(l-1))]\n",
    "        # for i,j in dic.keys():\n",
    "        #     s += mat[i][j]\n",
    "        # return s\n",
    "        return sum([x for x in dic.values()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        length = len(mat)\n",
    "        if length == 1:\n",
    "            # 单个元素\n",
    "            return mat[0][0]\n",
    "        flag = length % 2\n",
    "        sum_a = 0\n",
    "        sum_b = 0\n",
    "        all_sum = 0\n",
    "        if flag == 0:\n",
    "            # 偶数\n",
    "            for i in range(length):\n",
    "                j = length - (i+1)\n",
    "                sum_a = sum_a + mat[i][i]\n",
    "                sum_b = sum_b + mat[i][j]\n",
    "            all_sum = sum_a + sum_b\n",
    "        else:\n",
    "            # 基数\n",
    "            for i in range(length):\n",
    "                j = length - (i+1)\n",
    "                sum_a = sum_a + mat[i][i]\n",
    "                sum_b = sum_b + mat[i][j]\n",
    "            all_sum = sum_a + sum_b\n",
    "            # 减去中心值\n",
    "            midle_index = int(length/2)\n",
    "            midle_number = mat[midle_index][midle_index]\n",
    "            all_sum = all_sum - midle_number\n",
    "        return all_sum\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        lg = len(mat)\n",
    "        total = 0\n",
    "        for idx, sub in enumerate(mat):\n",
    "            if idx != lg-1-idx:\n",
    "                total += sub[idx] + sub[lg-1-idx]\n",
    "            else:\n",
    "                total += sub[idx]\n",
    "        return total\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        return sum(mat[i][j] for i in range(n) for j in range(n) \\\n",
    "                    if i == j or i + j == n - 1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        return sum(mat[i][j] for i in range(n) for j in range(n) \\\n",
    "                    if i == j or i + j == n - 1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat[0])\n",
    "        x = 0\n",
    "        for i in range(n):\n",
    "            x += mat[i][i]\n",
    "            x += mat[i][n-1-i]\n",
    "        if n%2==1:\n",
    "            x -= mat[int((n-1)/2)][int((n-1)/2)]\n",
    "        return x\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        l=len(mat)-1\n",
    "        a=0\n",
    "        res=0\n",
    "        while l!=a:\n",
    "            t=mat[a][a]+mat[a][l]+mat[l][l]+mat[l][a]\n",
    "            res+=t\n",
    "            a+=1\n",
    "            l-=1\n",
    "            if l<a:\n",
    "                break\n",
    "        if a==l:\n",
    "            res+=mat[a][a]\n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n=len(mat)\n",
    "        sum=0\n",
    "        flag=0\n",
    "        if n%2!=0:\n",
    "            flag=1\n",
    "        if flag==1:\n",
    "            for i in range(n):\n",
    "                sum+=mat[i][i]\n",
    "                if i!=((n-1)/2):\n",
    "                    sum+=mat[n-1-i][i]\n",
    "        else:\n",
    "            for i in range(n):\n",
    "                sum+=mat[i][i]\n",
    "                sum+=mat[n-1-i][i]\n",
    "        return sum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        a = [mat[i][i] for i in range(len(mat))]\n",
    "        b = [mat[i][-(i + 1)] for i in range(len(mat))]\n",
    "        if len(mat) % 2:\n",
    "            return sum(a) + sum(b) - a[len(mat)//2]\n",
    "        else:\n",
    "            return sum(a) + sum(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        total = 0\n",
    "\n",
    "        if len(mat) == 1:\n",
    "            return mat[0][0]\n",
    "        if len(mat) %2 == 0:\n",
    "            # 主对角线\n",
    "            j=0\n",
    "            for i in range(len(mat)):\n",
    "                total += mat[i][i]\n",
    "            for i in range(len(mat)-1,-1,-1):\n",
    "                total += mat[i][j]\n",
    "                j+=1\n",
    "            return total\n",
    "\n",
    "        if len(mat) %2 != 0:\n",
    "            # 副对角线\n",
    "            j=0\n",
    "            for i in range(len(mat)):\n",
    "                total += mat[i][i]\n",
    "            for i in range(len(mat)-1,-1,-1):\n",
    "                total += mat[i][j]\n",
    "                j+=1\n",
    "            return total - mat[len(mat)//2][len(mat)//2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n=len(mat)\n",
    "        Sum=0\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if i==j or i+j==n-1:\n",
    "                    Sum+=mat[i][j]\n",
    "        return Sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n=len(mat)\n",
    "        return sum(mat[i][j] for i in range(n) for j in range(n) if i==j or i+j==n-1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        ll = len(mat)\n",
    "        sum = 0\n",
    "        for i in range(ll):\n",
    "            sum += mat[i][i]\n",
    "        for i in range(ll):\n",
    "            sum += mat[ll - 1 - i][i]\n",
    "        print(sum)\n",
    "        if ll // 2 * 2 == ll:\n",
    "            return sum\n",
    "        else:\n",
    "            return sum - mat[ll // 2][ll//2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        if len(mat) % 2:\n",
    "            return sum([mat[i][len(mat) - i - 1] for i in range(len(mat))]) + sum([mat[i][i] for i in range(len(mat))]) - mat[len(mat) // 2][len(mat) // 2]\n",
    "        return sum([mat[i][len(mat) - i - 1] for i in range(len(mat))]) + sum([mat[i][i] for i in range(len(mat))])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        val = 0\n",
    "        length = len(mat)\n",
    "        for k in range(length):\n",
    "            if not length-k-1==k:\n",
    "                val = val+mat[k][k]+mat[k][length-1-k]\n",
    "            else:val = val + mat[k][k]\n",
    "        return val"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        m=len(mat)\n",
    "        ans=0\n",
    "        for i in range(m):\n",
    "                ans += mat[i][i]\n",
    "        for j in range(m):\n",
    "                ans += mat[j][m-1-j]\n",
    "        if m%2 == 0:\n",
    "            return ans\n",
    "        else:\n",
    "            return ans - mat[(m//2)][(m//2)]\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        l = len(mat)\n",
    "        s = 0\n",
    "        dic = {}\n",
    "        for i in range(l):\n",
    "            # s = s + mat[i][i] + mat[i][abs(i-(l-1))]\n",
    "            dic[(i,i)] = 'k'\n",
    "            dic[(i,abs(i-(l-1)))] = 'l'\n",
    "        for i,j in dic.keys():\n",
    "            s += mat[i][j]\n",
    " \n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        sum=0\n",
    "        sum1=0\n",
    "        j=-1\n",
    "        z=mat[(len(mat)-1)//2][(len(mat)-1)//2]\n",
    "        for i in range(len(mat)):\n",
    "            sum+=mat[i][i]\n",
    "            sum1 =sum1+mat[i][j]\n",
    "            j-=1\n",
    "            if j==-len(mat)-1:\n",
    "                break\n",
    "        if len(mat)%2!=0: #3*3\n",
    "            return sum+sum1-z\n",
    "        else:\n",
    "            return sum+sum1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        summ = 0\n",
    "        for i in range(n):\n",
    "            summ += mat[i][i] + mat[n-i-1][i]\n",
    "        if n % 2 != 0:\n",
    "            summ -= mat[n // 2][n // 2]\n",
    "        return summ"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        if(n == 1):\n",
    "            return mat[0][0]\n",
    "\n",
    "        cum_sum = 0\n",
    "        for i in range(n):\n",
    "            cum_sum += (mat[i][i] + mat[i][n-1-i])\n",
    "        if(n % 2 == 0):\n",
    "            return cum_sum\n",
    "        else:\n",
    "            return cum_sum - mat[int((n-1)/2)][int((n-1)/2)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        n=len(mat)\n",
    "        if len(mat)%2!=0:\n",
    "            a=[mat[i][i] for i in range(0,n)]\n",
    "            b=[mat[i][n-1-i] for i in range(0,n)]\n",
    "            return sum(a)+sum(b)-mat[(n-1)//2][(n-1)//2]\n",
    "        else:\n",
    "            a=[mat[i][i] for i in range(0,n)]\n",
    "            b=[mat[i][n-1-i] for i in range(0,n)]\n",
    "            return sum(a)+sum(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        l = len(mat)\n",
    "        dic = {}\n",
    "        for i in range(l):\n",
    "            dic[(i,i)] = mat[i][i]\n",
    "            dic[(i,abs(i-(l-1)))] = mat[i][abs(i-(l-1))]\n",
    "        return sum([x for x in dic.values()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        mats=mat[::-1]\n",
    "        a=[]\n",
    "        b=[]\n",
    "        for i in range(len(mat)):\n",
    "            a.append(mat[i][i])\n",
    "            a.append(mats[i][i])\n",
    "            if len(mat)%2==1:\n",
    "                # if mat[i][i]==mats[i][i]:\n",
    "                    b.append(mat[(len(mat)-1)//2][(len(mat)-1)//2])\n",
    "        if len(b)==0:\n",
    "            return sum(a)\n",
    "        else:\n",
    "            return (sum(a)-b[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        s=0\n",
    "        m= len(mat)\n",
    "        if m%2==0:\n",
    "            for i in range(m):\n",
    "                s+=mat[i][i]\n",
    "                s+=mat[i][m-i-1]\n",
    "        if m%2!=0:\n",
    "            for i in range(m):\n",
    "                s+=mat[i][i]\n",
    "                s+=mat[i][m-i-1]\n",
    "            s-=mat[int((m-1)/2)][int((m-1)/2)]\n",
    "        return s\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        m = len(mat)\n",
    "        ans = 0\n",
    "        for i in range(m):\n",
    "            for j in range(m):\n",
    "                if i==j or i+j==m-1:\n",
    "                    ans += mat[i][j]\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        ans = 0\n",
    "        lm = len(mat)\n",
    "        for i in range(lm):\n",
    "            ans += (mat[i][i] + mat[i][lm-1-i])\n",
    "        if lm % 2:\n",
    "            hf = lm // 2\n",
    "            ans -= mat[hf][hf]\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "\n",
    "        n = len(mat)\n",
    "        total = 0\n",
    "        mid = n // 2\n",
    "        for i in range(n):\n",
    "            total += mat[i][i] + mat[i][n - 1 - i]\n",
    "        return total - mat[mid][mid] * (n & 1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        res,left,right = 0,0,len(mat[0])-1\n",
    "        for m in mat:\n",
    "            if left == right:\n",
    "                res += m[left]\n",
    "            else:\n",
    "                res += m[left] + m[right]\n",
    "            left += 1\n",
    "            right -= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        sum = 0\n",
    "        for i in range(len(mat)):\n",
    "            sum += mat[i][i]\n",
    "        for i in range(len(mat)):\n",
    "            sum += mat[i][len(mat)-i-1]\n",
    "        if len(mat) % 2 != 0:\n",
    "            sum -= mat[len(mat)//2][len(mat)//2]\n",
    "        return sum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def diagonalSum(self, mat: List[List[int]]) -> int:\n",
    "        # s = 0\n",
    "        # n = len(mat)\n",
    "        # for i in range(n):\n",
    "        #     for j in range(n):\n",
    "        #         if i == j or i + j == n - 1:\n",
    "        #             s += mat[i][j]\n",
    "        \n",
    "        # return s\n",
    "        ans = 0\n",
    "        n = len(mat)\n",
    "        for i in range(len(mat)):\n",
    "            ans += mat[i][i]\n",
    "        for j in range(len(mat)):\n",
    "            ans += mat[j][len(mat) - 1 - j]\n",
    "        if len(mat) % 2 == 1:\n",
    "            ans -= mat[int(n / 2)][int(n / 2)]\n",
    "        return ans"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
